package com.yxq.mdt.app.modules.personal.model;

import com.alibaba.fastjson.JSON;
import com.yxq.mdt.app.base.RespBase;
import com.yxq.mdt.app.base.ibase.IBaseModel;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.modules.login.bean.UserBean;
import com.yxq.mdt.app.modules.personal.bean.AddressBean;
import com.yxq.mdt.app.modules.personal.bean.AddressListBean;
import com.yxq.mdt.app.modules.personal.bean.UserIndexBean;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AddressModel implements IBaseModel {
    @Override
    public void onDestroy() {

    }

    public static void district(ApiModel.CommonCallback<List<AddressBean>> callback) {
        RetrofitFactory.getRetrofit().create(AddressApi.class)
                .district(Constant.getToken())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<List<AddressBean>>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<List<AddressBean>> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void addressList(ApiModel.CommonCallback<AddressListBean> callback) {
        RetrofitFactory.getRetrofit().create(AddressApi.class)
                .addressList(Constant.getToken())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            AddressListBean addressListBean = JSON.parseObject(respBase.getData(), AddressListBean.class);
                            callback.onSuccess(addressListBean);
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void addressSave(String address_id, String name, String mobile, String province_id, String city_id, String district_id, String detail, ApiModel.CommonCallback<AddressListBean.ListBean> callback) {
        RetrofitFactory.getRetrofit().create(AddressApi.class)
                .addressSave(Constant.getToken(), address_id, name, mobile, province_id, city_id, district_id, detail)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<AddressListBean.ListBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<AddressListBean.ListBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void addressDelete(String address_id, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(AddressApi.class)
                .addressDelete(Constant.getToken(), address_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void setDefault(String address_id, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(AddressApi.class)
                .setDefault(Constant.getToken(), address_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public static void userIndex(ApiModel.CommonCallback<UserIndexBean> callback) {
        RetrofitFactory.getRetrofit().create(AddressApi.class)
                .userIndex(Constant.getToken())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<UserIndexBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<UserIndexBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }
}
